package com.wzq.app.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.wzq.app.R;
import com.wzq.app.base.BaseFragment;
import com.wzq.app.constant.App;
import com.wzq.app.constant.EventBusTag;
import com.wzq.app.manager.UserManager;
import com.wzq.app.model.UserInfo;
import com.wzq.app.ui.MyEvaActivity;
import com.wzq.app.ui.MyFeedBackActivity;
import com.wzq.app.ui.UserInfoActivity;
import com.wzq.app.utils.LaunchUtils;
import com.wzq.app.utils.ToastUtils;

import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/3/26.
 */

public class MineFragment extends BaseFragment {

    @BindView(R.id.mine_icon)
    ImageView mineIcon;
    @BindView(R.id.mine_name)
    TextView mineName;
    @BindView(R.id.mine_phone)
    TextView minePhone;
    @BindView(R.id.mine_info)
    RelativeLayout mineInfo;
    @BindView(R.id.go_login_layout)
    LinearLayout goLogin;
    @BindView(R.id.login_out)
    TextView login_out;
    @BindView(R.id.head_left_text)
    TextView headLeftText;
    @BindView(R.id.title)
    RelativeLayout title;
    @BindView(R.id.mine_collect)
    TextView mineCollect;
    @BindView(R.id.mine_setting)
    TextView mineSetting;
    @BindView(R.id.setting_version)
    TextView settingVersion;
    @BindView(R.id.version)
    TextView version;
    Unbinder unbinder;

    @Override
    public int getLayoutId() {
        return R.layout.mine_fragment;
    }

    @Override
    public void configView() {
        updateInfo(null);
    }

    @Override
    public boolean isUseEventBus() {
        return true;
    }

    @Subscriber(tag = EventBusTag.LOGIN_REFRESH)
    private void updateInfo(String text) {

        if (UserManager.getInstance().isLogin()) {
            UserInfo userInfo = UserManager.getInstance().getUser();
            mineInfo.setVisibility(View.VISIBLE);
            goLogin.setVisibility(View.GONE);
            mineName.setText(userInfo.getName());
            minePhone.setText(userInfo.getMobile());
            login_out.setVisibility(View.VISIBLE);
            if (App.TEACHER.equals(UserManager.getInstance().getUser().getType())) {
                mineSetting.setVisibility(View.VISIBLE);
            } else {
                mineSetting.setVisibility(View.GONE);
            }
        } else {
            mineInfo.setVisibility(View.GONE);
            goLogin.setVisibility(View.VISIBLE);
            login_out.setVisibility(View.GONE);
        }
    }

    @Override
    public void requestData() {

    }


    @OnClick(R.id.login_out)
    public void loginOut() {
        new MaterialDialog.Builder(mContext)
                .title("退出登录")
                .content("确认退出账号")
                .positiveText("取消")
                .negativeText("确定")
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        UserManager.getInstance().clear();
                        LaunchUtils.launchLoginGoMain(mContext);
                        getActivity().finish();
                    }
                }).show();
    }

    @OnClick({R.id.mine_collect, R.id.mine_setting, R.id.mine_info})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.mine_collect:
                ToastUtils.showToast(mContext, "我的评价");
                startActivity(new Intent(mContext, MyEvaActivity.class));
                break;
            case R.id.mine_setting:
                ToastUtils.showToast(mContext, "我的反馈");
                startActivity(new Intent(mContext, MyFeedBackActivity.class));
                break;
            case R.id.mine_info:
                startActivity(new Intent(mContext, UserInfoActivity.class));
                break;
            default:
                break;
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
